import {
    loginToMobile,
    loginToPass,
    resetPass,
    logout,
    getInfo
} from '@/api/login'

const user = {
    state: {
        token: uni.getStorageSync('token') || '',
        mobile: uni.getStorageSync('mobile') || {},
        setInfo: uni.getStorageSync('userInfo') || {},
        isToLogin: false, //是否前往登录
        loginPopupShow: false, //控制是否打开登录弹窗
        showWxLogin: false, //微信授权登录弹窗-新2022-10
        loadingShow: false, //加载动画
        userInfo: {},
        tel: ""
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_MOBILE: (state, mobile) => {
            state.mobile = mobile
        },
        SET_INFO: (state, setInfo) => {
            state.setInfo = setInfo
        },
         SET_ISToLOGIN: (state, isToLogin) => {
                    console.log(123)
                    state.isToLogin = isToLogin
                },
        setLoginPopupShow: (state, data) => {
            state.loginPopupShow = data
        },
        setShowWxLogin: (state, data) => {
            state.showWxLogin = data
        },
        setUserInfo: (state, data) => {
            if (data) {
                state.userInfo = Object.assign({}, state.userInfo, data);
                console.log(state.userInfo, "state.userInfo")
            }
        },
        SET_TEL: (state, tel) => {
            state.tel = tel
        },
    },
    actions: {
        // 账号密码登录
        passLogin({
            commit
        }, userInfo) {
            userInfo.mobile = userInfo.mobile.trim()
            return new Promise((resolve, reject) => {
                loginToPass(userInfo).then(res => {
                    if (res.code == 1) {
                        console.log(res, 777)
                        uni.setStorageSync('token', res.data.token)
                        commit('SET_TOKEN', res.data.token)
                        uni.setStorageSync('mobile', userInfo.mobile)
                        commit('SET_MOBILE', userInfo.mobile)
                        resolve(res)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 手机号登录
        mobileLogin({
            commit
        }, userInfo) {
            userInfo.mobile = userInfo.mobile.trim()
            return new Promise((resolve, reject) => {
                loginToMobile(userInfo).then(res => {
                    uni.setStorageSync('token', res.data.token)
                    commit('SET_TOKEN', res.data.token)
                    uni.setStorageSync('mobile', userInfo.mobile)
                    commit('SET_MOBILE', userInfo.mobile)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        resetLogin({
            commit
        }, userInfo) {
            userInfo.mobile = userInfo.mobile.trim()
            return new Promise((resolve, reject) => {
                resetPass(userInfo).then(res => {
                    console.log(res, 145)
                    uni.setStorageSync('token', res.data.token)
                    commit('SET_TOKEN', res.data.token)
                    uni.setStorageSync('mobile', userInfo.mobile)
                    commit('SET_MOBILE', userInfo.mobile)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                getInfo({
                    token: state.token,
                    reqtype: uni.getStorageSync('platform') == 'ios' ? 'IOS' : 'Android'
                }).then(res => {
                    const user = res.data;
                    // user.imagePath = user.imagePath == "" ? require("@/static/mine/logo.png") : process.env.VUE_APP_BASE_API +
                    // 	user.imagePath;
                    uni.setStorageSync('userInfo', user)
                    commit('SET_INFO', user)
                    resolve(res.data)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                resolve()
                logout(state.token).then(() => {
                    // uni.setStorageSync('mobile', '')
                    // commit('SET_TOKEN', '')
                    // uni.setStorageSync('token', '')
                    // commit('SET_TOKEN', '')
                    // uni.setStorageSync('userInfo', '')
                    uni.removeStorageSync("userInfo");
                    uni.removeStorageSync("token");
                    uni.removeStorageSync("mobile");
                    uni.clearStorage();
                    commit('SET_INFO', '')
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 前端 登出
        FedLogOut({
            commit
        }) {
            return new Promise(resolve => {
                uni.setStorageSync('token', '')
                commit('SET_TOKEN', '')
                uni.setStorageSync('userInfo', '')
                commit('SET_INFO', '')
                resolve()
            })
        }
    }
}

export default user